/*
 *  Copyright 2010 netjois.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.userinterface.impl;

import edu.upf.network.ContentTransfer;
import edu.upf.utils.Definitions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author netjois
 */
public class DownloadsTableModel extends AbstractTableModel implements Definitions {

    private List<ContentTransfer> datalist = new ArrayList();
    private String[] columns = {"Name", "Type", "Size", "Status","Progress"};

    @Override
    public int getRowCount() {
        return datalist.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int col) {
        return columns[col];
    }

    public void addTransfer(ContentTransfer newTransfer) {
        datalist.add(newTransfer);
        fireTableDataChanged();
    }

    public ContentTransfer getDescriptionContentAt(int row) {
        return datalist.get(row);
    }

    public ContentTransfer removeDescriptionContentAt(int row) {
        ContentTransfer dc = datalist.remove(row);
        fireTableDataChanged();
        return dc;
    }

    @Override
    public Class getColumnClass(int column){
        return Object.class;
    }

    @Override
    public Object getValueAt(int row, int col) {
        ContentTransfer currentContent = (ContentTransfer) datalist.get(row);
        switch (col) {
            case 0:
                return currentContent.getContentName();
            case 1:
                return currentContent.getType();
            case 2:
                return currentContent.getContentSize();
            case 3:
                return currentContent.getContentStatus();
            case 4:
                return currentContent.getProgress();
            default:
                return null;
        }
    }

    public void clearCompleted(){
        Iterator<ContentTransfer> i = datalist.iterator();
        while(i.hasNext()){
            if(i.next().getContentStatus().equals(FINISHED)){
                i.remove();
            }
        }
        fireTableDataChanged();
    }

    public void refreshDownloadData(){
        fireTableDataChanged();
    }
}
